package org.shean.app.rssreader;

import org.shean.app.rssreader.provider.SzRSSReader;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class SzRSSPostTabActivity extends TabActivity implements TabHost.TabContentFactory {

	private TabSpec unread;
	private TabHost mTabHost;
	private Uri mUri;
	private long mId;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mUri = getIntent().getData();
        mId = Long.parseLong(mUri.getPathSegments().get(1));
        mTabHost = getTabHost();
		unread = mTabHost.newTabSpec("tab1")
        .setIndicator("Unread")
        .setContent(new Intent("org.shean.rssreader.action.VIEW_UNREAD", mUri));

		mTabHost.addTab(unread);
		mTabHost.addTab(mTabHost.newTabSpec("tab2")
                .setIndicator("All")
                .setContent(new Intent("org.shean.rssreader.action.VIEW_ALL", mUri)));
		mTabHost.addTab(mTabHost.newTabSpec("tab3")
                .setIndicator("Favorite")
                .setContent(new Intent("org.shean.rssreader.action.VIEW_FAVORITE", mUri)));

//		TextView textview = (TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
//		textview.setText("ABC");
	}
	
	
	@Override
	protected void onStart()
	{
		super.onStart();
		updatePostCount();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		mTabHost.setCurrentTab(1);
		
		IntentFilter f = new IntentFilter();
        f.addAction("org.shean.rssreader.POST_STATUSCHANGED");
        registerReceiver(mStatusListener, new IntentFilter(f));
	}
	
    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(mStatusListener);
    }
    
    private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // this receiver is only used for META_CHANGED events
        	updatePostCount();
        }
    };
	
	private void updatePostCount()
	{
		String[] cols = new String[]{"COUNT(*)"};
		Uri search_uri = mUri;
		
		if(mId == -1)
			search_uri = SzRSSReader.Posts.CONTENT_URI;
		
		Cursor c = this.managedQuery(search_uri, cols, null, null, null);
		c.moveToFirst();
		int all_count = c.getInt(0);
		c.close();
		
		c = this.managedQuery(search_uri, cols, SzRSSReader.Posts.IS_READ + "=0", null, null);
		c.moveToFirst();
		int unread_count = c.getInt(0);
		c.close();
		
		c = this.managedQuery(search_uri, cols, SzRSSReader.Posts.IS_FAVORITE + "=1", null, null);
		c.moveToFirst();
		int favorite_count = c.getInt(0);
		c.close();
		TextView textview = (TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
		textview.setText("Unread("+unread_count + ")");
		
		textview = (TextView)mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
		textview.setText("All("+all_count + ")");
		
		textview = (TextView)mTabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
		textview.setText("Favorite("+favorite_count + ")");
	}

    /** {@inheritDoc} */
    public View createTabContent(String tag) {
        final TextView tv = new TextView(this);
        tv.setText("Content for tab with tag " + tag);
        return tv;
    }
}
